﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Fakebook.Codes;
using System.Data;
using System.Data.OleDb;
using Fakebook.Shared;
using Fakebook.Shared.FakebookDataSetTableAdapters;

namespace Fakebook
{
    public partial class DemoADONET : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //Connected();
                //Disconnected();
                TypeDataSet();
            }
        }


        private void TypeDataSet()
        {
            FakebookDataSet ds = new FakebookDataSet();

            PostTableAdapter adap = new PostTableAdapter();
            adap.Fill(ds.Post);





            foreach (DataRow i in ds.Post.Rows)
            {

                

                Response.Write(
                    string.Format("กระทู้เรื่อง {0} <br />", i[ds.Post.HeadlineColumn])
                );
                i[ds.Post.HeadlineColumn] = "BBBB";
            }

            adap.Update(ds);

            foreach (DataRow i in ds.Post.Rows)
            {



                Response.Write(
                    string.Format("กระทู้เรื่อง {0} <br />", i[ds.Post.HeadlineColumn])
                );
            }


        }

        private void Disconnected()
        {

    

            SqlDataAdapter adap = new SqlDataAdapter("select * from [Post]", new SqlConnection(WebHelper.ConnectionString));
            DataTable source = new DataTable();
            adap.Fill(source);
            foreach (DataRow i in source.Rows)
            {
                Response.Write(
                    string.Format("กระทู้เรื่อง {0} <br />", i["Headline"])
                );
            }

        }

        private void Connected()
        {

            DataTable source = new DataTable();
            using (SqlCommand cmd = new SqlConnection(WebHelper.ConnectionString).CreateCommand())
            {
                cmd.CommandText = @"

select * from [Post] where Headline like '%'+@Headline+'%'

";

                cmd.Parameters.AddWithValue("Headline","tes");
                cmd.CommandType = System.Data.CommandType.Text;
                if (cmd.Connection.State != System.Data.ConnectionState.Open)
                {
                    cmd.Connection.Open();
                }
                //SqlDataReader reader = cmd.ExecuteReader();
                //while (reader.Read())
                //{
                //    Response.Write(
                //        string.Format("กระทู้เรื่อง {0} <br />", reader["Headline"])
                //    );
                //}
                //reader.Dispose();
                source.Load(cmd.ExecuteReader());

            }
            foreach (DataRow i in source.Rows)
            {
                Response.Write(
                    string.Format("กระทู้เรื่อง {0} <br />", i["Headline"])
                );
            }
        }

    }
}
